package gwtadmin.client;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Widget;

public class Table extends FlexTable {

	public Table() {
		setText(0, 0, "");
		
		getCellFormatter().setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		
		getRowFormatter().setStyleName(0, "gwt-TableHeader");
		setStyleName("gwt-Table");
	}
	
	/* returns item index [0,itemRowCount) */
	public int addItemRow() {
		applyItemRowStyle(itemRowCount);
		return itemRowCount++;
	}
	
	public int getItemCount() {
		return itemRowCount;
	}
	
	public int getSvcCount() {
		return svcRowCount;
	}

	/* returns service row index [0,svcRowCount) */
	public int addSvcRow(Widget w) {
		setWidget(svcRowCount + 1, 0, w);
		applySvcRowStyle(svcRowCount);
		return svcRowCount++;
	}
	
	public void clearItems() {
		int itemCount = svcRowCount + itemRowCount + 1;
		for (int i = svcRowCount + 1; i < itemCount; i++) {
			removeRow(svcRowCount + 1);
		}
		itemRowCount = 0;
	}
	
	public void setItem(int itemIndex, int column, String text) {
		setText(itemIndex + svcRowCount + 1, column, text);
	}
	
	public void setItem(int itemIndex, int column, Widget w) {
		setWidget(itemIndex + svcRowCount + 1, column, w);
	}
	
	/* returns service row index [0,svcRowCount) */
	public int addSvcRow(String text) {
		setText(svcRowCount + 1, 0, text);
		applySvcRowStyle(svcRowCount);
		return svcRowCount++;
	}

	public int addColumn(String title) {
		setText(0, columnCount, title);
		columnCount++;
		
		updateFormat();
		return (columnCount-1);
	}
	
	public boolean setFilter(int column, String filter) {
		if (itemRowCount == 0)
			return false;

		if (filter.equals("")) {
			showItems(true);
			return true;
		}

		filter = filter.toLowerCase();
		int cnt = 0;

		for (int i = 0; i < itemRowCount; i++) {
			if (!getText(i + svcRowCount + 1, column).toLowerCase().contains(filter)) {
				showItem(i, false);
				cnt++;
			} else
				showItem(i, true);
		}
		
		return (cnt < itemRowCount);
	}
	
	public void showItems(boolean show) {
		for (int i = 0; i < itemRowCount; i++) {
			showItem(i, show);
		}
	}
	
	public String getItemText(int itemIndex, int column) {
		return getText(itemIndex + svcRowCount + 1, column);
	}
	
	public void showItem(int itemIndex, boolean show) {
		getRowFormatter().setVisible(itemIndex + svcRowCount + 1, show);
	}
	
	public void showSvc(int svcIndex, boolean show) {
		getRowFormatter().setVisible(svcIndex + 1, show);
	}
	
	public void showHeader(boolean show) {
		getRowFormatter().setVisible(0, show);
	}
	
	private void applySvcRowStyle(int svcIndex) {
		svcIndex++;
		getCellFormatter().getElement(svcIndex, 0).setAttribute("colSpan",
				String.valueOf(columnCount));
		getCellFormatter().setHorizontalAlignment(svcIndex, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

		getCellFormatter().setStyleName(svcIndex, 0, "gwt-TableCell");
	}
	
	private void applyFirstCellStyle(int itemIndex) {
		itemIndex += svcRowCount + 1;
		getCellFormatter().getElement(itemIndex, 0).removeAttribute("colSpan");
		getCellFormatter().setHorizontalAlignment(itemIndex, 0,
				HasHorizontalAlignment.ALIGN_RIGHT);

		getCellFormatter().setStyleName(itemIndex, 0, "gwt-TableCell");
	}
	
	private void applyItemRowStyle(int itemIndex) {
		int index = itemIndex + svcRowCount + 1;
		
		for (int i = 0; i < columnCount; i++)
			getCellFormatter().setStyleName(index, i, "gwt-TableCell");
		
		applyFirstCellStyle(itemIndex);
	}
	
	private void updateFormat() {
		for (int i = 0; i < columnCount; i++)
			getCellFormatter().setStyleName(0, i, "gwt-TableHeaderCell");
		
		for (int i = 0; i < svcRowCount; i++)
			applySvcRowStyle(i);
		
		for (int i = 0; i < itemRowCount; i++)
			applyFirstCellStyle(i);
			
	}

	private int itemRowCount = 0;
	private int svcRowCount = 0;
	private int columnCount = 0;
}
